package Snackbarsystem;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class userFood {
    public void user() throws Exception {
        while (true) {
            System.out.println("-----小吃店用户系统-----");
            System.out.println("1.查看和订购小吃");
            System.out.println("2.查看和管理订单");
            System.out.println("3.查看订餐电话");
            System.out.println("4.查看本店介绍");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            Scanner ch = new Scanner(System.in);
            String choose = ch.next();
            if (choose.equals("1")) {
                food();
            }
            else if(choose.equals("2")){
               order();
            }
           else if (choose.equals("3")) {
                System.out.println("订餐热线：100820-820-dhc");
            }
           else if (choose.equals("4")) {
                try (
                        BufferedReader bf = new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\Food.txt"));
                ) {
                    String introduce;
                    while ((introduce = bf.readLine()) != null)
                    {
                        System.out.println(introduce);
                    }
                }
            }
           else if (choose.equals("5")) {
                password();
            }
           else if (choose.equals("6")) {
                System.out.println("感谢使用小京系统");
                break;
            }
           else {
                System.out.println("无此功能，请重新输入！");
            }
        }
    }


    public void food()throws Exception{
        System.out.println("----欢迎查看小吃----");
        System.out.println("编号:\t小吃名:\t   价格:\t    口味:");
        try (
                BufferedReader out =new BufferedReader (new FileReader("D:\\idea java\\Foodsystem\\src\\File\\food1.txt"));
                BufferedWriter in=new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\order.txt"))
        ) {
            List<String> buyf=new ArrayList<String>();//储存所读到的小吃
            String check;
            while ((check = out.readLine()) != null) {
                buyf.add(check);
                System.out.println(check);
                //输出每一行food1中小吃
            }
            Scanner sc=new Scanner(System.in);
            System.out.println("---欢迎选购小吃---");
            System.out.println("请选择所选小吃编号：");
            while (true){
                String num=sc.next();
                if (num.equals("over")){
                    System.out.println("选购成功！欢迎再次选购！");
                    break;
                }
                else{
                    int n=0;
                    try {
                        n=Integer.parseInt(num);
                    }catch (NumberFormatException e){
                    }
                    if (n<=buyf.size()&&n>0) {
                        in.write(buyf.get(n - 1) + "\n");//将选中菜单输入缓冲流
                    }else {
                        System.out.println("输入有误!请重新输入!");
                    }
                }
            }
            in.flush();//打印缓冲流到order文件
        }
    }
    public void order()throws Exception{
        List<String> so=new ArrayList<String>();//储存订单
        List<String> ao=new ArrayList<String>();//储存总菜单
        try(
                BufferedReader ro=new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\order.txt"));
                BufferedReader ao_1 =new BufferedReader (new FileReader("D:\\idea java\\Foodsystem\\src\\File\\food1.txt"));
        ) {
            String check;
            while ((check = ao_1.readLine()) != null) {
                ao.add(check);//将所有小吃存入菜单
            }

            String reorder;
            int jilu = 0;
            System.out.println("编号:\t小吃名:\t   价格:\t    口味:");
            while ((reorder = ro.readLine()) != null) {
                so.add(reorder);
                jilu++;
                System.out.println(reorder);//输出每一行order中小吃
            }

            System.out.println("--管理订单--");
            System.out.println("请选择功能:" + "\n" + "1.添加    2.删除    3.更换   4.确认");
            Scanner number = new Scanner(System.in);
            String choice = number.next();

            if (choice.equals("2")) {
                System.out.println("请选择删除第几个小吃:");
                int num_1 = number.nextInt();
                if (num_1 <= jilu) {
                    so.remove(num_1 - 1);
                    System.out.println("删除成功!");
                }else {
                    System.out.println("无此小吃!");
                }
            } else if (choice.equals("1")) {
                System.out.println("请输入添加小吃编号:");
                int num_2 = number.nextInt();
                so.add(ao.get(num_2 - 1));
                System.out.println("添加成功!");
            } else if (choice.equals("3")) {
                System.out.println("请输入要跟换第几个小吃:");
                int n = number.nextInt();
                if (n<=jilu){
                System.out.println("请输入想跟换的小吃编号:");
                int num_3 = number.nextInt();
                so.set(n - 1, ao.get(num_3 - 1));
                System.out.println("跟换成功!");
                }else{
                    System.out.println("无此小吃!");
                }
            } else if (choice.equals("4")) {
                System.out.println("下单成功,感谢您的信任!");
            } else {
                System.out.println("无此功能!");
                if (so.isEmpty() != false) {
                    for (int i = 0; i < so.size(); i++) {
                        so.remove(i);
                    }
                }
            }
        }
        if (so.isEmpty()==false) {
            try (
                    BufferedWriter co = new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\order.txt"));
            ) {
                for (int i = 0; i < so.size(); i++) {
                    co.write(so.get(i) + "\n");
                }
                co.flush();
            }
        }
        else{
            System.out.println("还未选择小吃!");
        }
    }
    public void password()throws Exception{
        LinkedList<String> nwo = new LinkedList<String>();//储存新、旧密码
        List<String> judment=new ArrayList<String>();//储存判断原密码是否正确
        try (
                BufferedReader br0 = new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"));
                BufferedReader br1 = new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"));
                BufferedReader br2 = new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\Account.txt"));
        ) {
            int co = 0;
            while ((br0.readLine()) != null) {
                co++;//得到密码、账号行数
            }

//            int account_han=0;//储存账户对应行数
//            allAccount app=new allAccount();
//            String test_account= app.main_account;
//            System.out.println(app.main_account);
//            for (int j=1;j<=co;j++){
//                if (test_account.equals(br2.readLine())){
//                    account_han=j;
//                }
//            }获取账户遇到困难,无法调用值过来

            Scanner sc = new Scanner(System.in);
            System.out.println("请输入旧密码：");
            String oldpassword = sc.next();
            for (int i = 1; i <= co; i++) {
                String old = br1.readLine();
                if (oldpassword.equals(old) ){//&& i == account_han){仅当输入账户对应的旧密码才算正确
                    String jud = "yes";
                    judment.add(jud);
                    System.out.println("旧密码正确");
                    try (
                            BufferedReader br = new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"));
                    ) {
                        Scanner nw = new Scanner(System.in);
                        System.out.println("请输入新密码：");
                        String newword = nw.next();
                        for (int j = 1; j <= co; j++) {
                            nwo.add(br.readLine());
                        }
                        nwo.remove(i-1);//移除旧密码
                        nwo.add(i-1 , newword);//将新密码填入容器对应位置
                    }
                    try (
                            BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"));
                    ) {
                        for (int j = 0; j < nwo.size(); j++) {
                            String word = nwo.get(j);//将容器中已更改完的密码赋值给word
                            bw.write(word + "\n");//写入passWord文档中
                        }
                        bw.flush();
                    }
                    System.out.println("修改成功！");
                }
            }
            if ((judment.isEmpty())==true)//表示原密码错误，容器judment未储存yes
            {
                System.out.println("旧密码错误!");
            }
        }
    }
}

